from selenium.webdriver.common.by import By
import time
from utils.base_aolai import BaseObject, BaseHandle


class AdressObject(BaseObject):
    def __init__(self):
        super().__init__()
        self.recipient = (By.ID, 'com.yunmall.lc:id/address_receipt_name')
        self.phone = (By.ID, 'com.yunmall.lc:id/address_add_phone')
        self.province = (By.ID, 'com.yunmall.lc:id/address_province')
        self.addr_info = (By.ID, 'com.yunmall.lc:id/address_detail_addr_info')
        self.address_post_code = (By.ID, 'com.yunmall.lc:id/address_post_code')
        self.address_default = (By.ID, 'com.yunmall.lc:id/address_default')
        self.send = (By.ID, 'com.yunmall.lc:id/button_send')
        self.out = (By.ID, 'com.yunmall.lc:id/ymtitlebar_left_btn_image')

    def findRecipients(self):
        return self.search_ele(self.recipient)

    def findPhone(self):
        return self.search_ele(self.phone)

    def findProvince(self):
        return self.search_ele(self.province)

    def findProvince_1(self, one):
        return self.search_ele((By.XPATH, "//*[contains(@text,'{}')]".format(one)))

    # def findProvince_2(self, two):
    #     return self.search_ele((By.XPATH, "//*[contains(@text,'{}')]".format(two)))
    #
    # def findProvince_3(self, three):
    #     return self.search_ele((By.XPATH, "//*[contains(@text,'{}')]".format(three)))

    def findProvince_list_A(self):
        return self.search_eles((By.ID, 'com.yunmall.lc:id/area_title'))

    # def findProvince_list_B(self):
    #     return self.search_eles((By.ID, 'com.yunmall.lc:id/area_title'))
    #
    # def findProvince_list_C(self):
    #     return self.search_eles((By.ID, 'com.yunmall.lc:id/area_title'))

    def findAddr_info(self):
        return self.search_ele(self.addr_info)

    def findaAdress_post_code(self):
        return self.search_ele(self.address_post_code)

    def findAddress_default(self):
        return self.search_ele(self.address_default)

    def findSend(self):
        return self.search_ele(self.send)

    def findOut(self):
        return self.search_ele(self.out)


class AdressHandle(BaseHandle):
    def __init__(self):
        self.ao = AdressObject()

    def Recipients(self, name):
        self.input(self.ao.findRecipients(), name)

    def Phones(self, num):
        self.input(self.ao.findPhone(), num)

    def Province_(self):
        self.ao.findProvince().click()

    def Province_A(self, one):
        self.ao.findProvince_1(one).click()

    # def Province_B(self, two):
    #     self.ao.findProvince_2(two).click()
    #
    # def Province_C(self, three):
    #     self.ao.findProvince_3(three).click()

    def Province_list_A(self):
        time.sleep(1)
        res = self.ao.findProvince_list_A()
        time.sleep(1)
        print([i.text for i in res])
        return [i.text for i in res]

    # def Province_list_B(self):
    #     time.sleep(1)
    #     res = self.ao.findProvince_list_B()
    #     time.sleep(1)
    #     print([i.text for i in res])
    #
    #     return [i.text for i in res]
    #
    # def Province_list_C(self):
    #     time.sleep(1)
    #     res = self.ao.findProvince_list_C()
    #     time.sleep(1)
    #     print([i.text for i in res])
    #
    #     return [i.text for i in res]

    def Addr_info(self, all):
        self.input(self.ao.findAddr_info(), all)

    def Adress_post_code(self, code):
        self.input(self.ao.findaAdress_post_code(), code)

    def Address_default(self):
        self.ao.findAddress_default().click()

    def Sent_A(self):
        self.ao.findSend().click()

    def out_A(self):
        self.ao.findOut().click()


class AdressTask:
    ah = AdressHandle()
    bh = BaseHandle()

    # 收件人 手机号
    @classmethod
    def inputAdress(cls, name, num, al):
        cls.ah.Recipients(name)
        cls.ah.Phones(num)
        cls.ah.Addr_info(al)

    @classmethod
    def inputname(cls, add):
        cls.ah.Province_()
        time.sleep(1)
        for i in add:
            print(i)
            for x in range(3):
                cls.bh.move_all(tag=2)
                print(i)
                if i in cls.ah.Province_list_A():
                    time.sleep(1)
                    cls.ah.Province_A(i)
                    break
                else:
                    cls.bh.move_all()

        time.sleep(1)
        # cls.bh.move_all(tag=2)

        # for i in range(3):
        #     if two in cls.ah.Province_list_A():
        #         time.sleep(2)
        #         cls.ah.Province_A(two)
        #         break
        #     else:
        #         cls.bh.move_all()
        #
        # time.sleep(1)
        # cls.bh.move_all(tag=2)
        #
        # for i in range(3):
        #
        #     if three in cls.ah.Province_list_A():
        #         time.sleep(1)
        #         cls.ah.Province_A(three)
        #         break
        #     else:
        #         cls.bh.move_all()


    # 邮编
    @classmethod
    def code(cls, code):
        cls.ah.Adress_post_code(code)

    # 默认地址
    @classmethod
    def default(cls):
        cls.ah.Address_default()

    # 退出
    @classmethod
    def sent_back(cls):
        cls.ah.Sent_A()
        cls.ah.out_A()
